var app = getApp();
// components/bindnumber/bindnumber.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    number: ""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  save: function () {
    if (this.data.number != "" && this.data.name != "") {
      if (this.data.number.length != 11) {
        wx.showToast({
          title: '输入的手机号有误，请重新输入',
          icon: 'none',
          duration: 1500
        })
      } else {
        let that = this;
        let data = {
          "customerPwd": that.data.name,
          "customerNumber": that.data.number
        };
        var url = "http://" + app.globalData.ipv4 + ":8080/customer/find";
        wx.request({
          url: url,
          data: data,
          method: "POST",
          header: { 'content-type': 'application/json' },
          success: function (res) {
            if (res.data != 0) {
              app.globalData.number = that.data.number;
              app.globalData.name = that.data.name;
              app.globalData.id = res.data;
              wx.switchTab({
                url: '../../pages/index/index',
              })
            } else {
              var url1 = "http://" + app.globalData.ipv4 + ":8080/customer/select"
              wx.request({
                url: url1,
                data: data,
                method: "POST",
                header: { 'content-type': 'application/json' },
                success: function (e) {
                  app.globalData.number = e.data.customerNumber;
                  app.globalData.name = e.data.customerName;
                  app.globalData.id = e.data.customerId;
                  wx.switchTab({
                    url: '../../pages/index/index',
                  })
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '输入的手机号有误，请重新输入',
              icon: 'none',
              duration: 1500
            })
          }
        })
      }
    } else {
      wx.showToast({
        title: '输入的手机号有误，请重新输入',
        icon: 'none',
        duration: 1500
      })
    }
  },
  bindNameInput: function (e) {
    let that = this
    that.setData({
      name: e.detail.value
    })
  },
  bindPhoneInput: function (e) {
    let that = this;
    var replaceArray = [];
    if(e.detail.value.length==11){
      for (let i = 0; i < e.detail.value.length; ++i) {//正则判断是否合法
        var textValue = (/^[0-9]$/.test(e.detail.value.charAt(i)));
        if (!textValue) {
          replaceArray.push(e.detail.value.charAt(i));
        }
      }
      if (replaceArray.length == 0){
        that.setData({
          number: e.detail.value
        })
      }else{
        wx.showToast({
          title: '输入的手机号有误，请重新输入',
          icon: 'none',
          duration: 1500
        })
      }
    }else{
      wx.showToast({
        title: '输入的手机号有误，请重新输入',
        icon: 'none',
        duration: 1500
      })
    }
    
  },
  showok: function () {
    wx.showToast({
      title: '失败',
      icon: 'loading',
      duration: 2000
    })
  },
  code: function () {

  }
})